package com.zhou.cloud.cousumer.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.Date;

/**
 * @author zhouyongjun
 * @Description:
 * @date 2021/9/2 17:43
 */
@RestController
@RequestMapping("/cousumer")
public class CousumerController {
    @Autowired
    RestTemplate restTemplate;
    @RequestMapping("get")
    @SentinelResource(value = "cousumer-get",fallback ="fallback",blockHandler = "flowException")
    public String test() {
        System.err.println("Cousumer test "+new Date());
        return restTemplate.getForObject("http://provider/provider/get" , String.class);
    }

    @RequestMapping("set")
    @SentinelResource(value = "cousumer-set",fallback ="fallback",blockHandler = "flowException")
    public String testSet() {
        System.err.println("Cousumer test "+new Date());
        return restTemplate.getForObject("http://provider/provider/set" , String.class);
    }

    public String fallback() {
        return "cousumer fail back...";
    }

    // 自定义异常必须与原方法返回值及参数完全一致
    public String flowException(BlockException e) {
        return "cousumer 限流了！";
    }

    // 自定义异常必须与原方法返回值及参数完全一致
    public String degradeException(BlockException e) {
        return " cousumer 降级了！";
    }
}
